package com.zhiyou.servlet;

import com.zhiyou.dao.HouseDao;
import com.zhiyou.model.House;
import com.zhiyou.service.HouseService;
import com.zhiyou.service.impl.HouseServiceImpl;
import com.zhiyou.util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class HouseServlet extends HttpServlet {
    private HouseService hS=new HouseServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        req.setCharacterEncoding("utf-8");
//        resp.setContentType("text/html;charset=utf-8");
        //接收参数
        String pageNoStr = req.getParameter("pageNo");
        System.out.println("pageNoStr="+pageNoStr);
        int pageNo = Integer.parseInt(pageNoStr);//第pageNo页
        System.out.println("pageNo="+pageNo);
        int pageSize=3;//每页展现的数量
        //接受模糊查询关键字
        String keyword = req.getParameter("keyword");
        String hdirS = req.getParameter("hdirS");
        String lowprice = req.getParameter("lowprice");
        String highprice = req.getParameter("highprice");
        //封装数据
        HashMap<String,String> keywordMap = new HashMap<>();
        keywordMap.put("keyword",keyword);
        keywordMap.put("hdirS",hdirS);
        keywordMap.put("lowprice",lowprice);
        keywordMap.put("highprice",highprice);

        int total = hS.getHouseNum(keywordMap);//记录信息的数量
        List<House> houses =hS.getHouse(pageNo,pageSize,keywordMap);
        //List<House> houses =new HouseDao().getHouse(pageNo,pageSize);

        //创建分页工具对象
        PageUtil pageUtil = new PageUtil(pageNo, total);
        req.getSession().setAttribute("houses",houses);
        req.getSession().setAttribute("pageUtil",pageUtil);
        req.getSession().setAttribute("keywordMap",keywordMap);
        System.out.println("日志: pageNo="+pageNo+" total="+total+" pageNum="+(total%pageSize==0?total/pageSize:(total/pageSize+1)));
        resp.sendRedirect(req.getContextPath()+"/view/flats/list.jsp");
    }
}
